<!-- 推送明细查询 -->
<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true">
            <el-form-item label="查询条件" prop="name">
                <el-input
                    v-model="queryParams.name"
                    placeholder="请输入实体名称"
                    clearable
                    @keyup.enter="handleQuery"
                ></el-input>
            </el-form-item>
            <el-form-item label="状态" prop="state">
                <el-select
                    v-model="queryParams.state"
                    placeholder="数据状态"
                    clearable
                    style="width: 240px"
                >
                    <el-option
                        v-for="dict in sys_normal_disable"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    />
                </el-select>
            </el-form-item>
        </el-form>
        <!-- button area -->
        <el-row :gutter="20" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    plain
                    icon="Search"
                    @click="handleQuery"
                    >查询</el-button
                >
            </el-col>
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" @click="handleAdd"
                    >新增</el-button
                >
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="success"
                    plain
                    icon="Edit"
                    :disabled="single"
                    @click="handleUpdate"
                    >修改</el-button
                >
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="danger"
                    plain
                    icon="Delete"
                    :disabled="multiple"
                    @click="handleDelete"
                    >删除</el-button
                >
            </el-col>
        </el-row>
        <el-table :data="tableData" :loading="loading">
            <el-table-column
                label="实体编码"
                align="center"
                prop="id"
            ></el-table-column>
            <el-table-column label="实体类型" align="center" prop="type">
                <template #default="scope">
                    <dict-tag
                        :options="auth_entity_type"
                        :value="scope.row.type"
                    /> </template
            ></el-table-column>
            <el-table-column
                label="实体名称"
                align="center"
                prop="name"
            ></el-table-column>
            <el-table-column label="状态" align="center" prop="state">
                <template #default="scope">
                    <el-switch
                        v-model="scope.row.state"
                        active-value="1"
                        inactive-value="0"
                        @change="handleStatusChange(scope.row)"
                    ></el-switch> </template
            ></el-table-column>
            <el-table-column
                label="创建时间"
                align="center"
                prop="createTime"
            ></el-table-column>
            <el-table-column
                label="操作"
                align="center"
                width="150"
                class-name="small-padding fixed-width"
            >
                <template #default="scope">
                    <el-tooltip
                        content="修改"
                        placement="top"
                        v-if="scope.row.id !== undefined"
                    >
                        <el-button
                            link
                            type="primary"
                            icon="Edit"
                            @click="handleUpdate(scope.row)"
                        ></el-button>
                    </el-tooltip>
                    <el-tooltip
                        content="删除"
                        placement="top"
                        v-if="scope.row.id !== undefined"
                    >
                        <el-button
                            link
                            type="primary"
                            icon="Delete"
                            @click="handleDelete(scope.row)"
                        ></el-button>
                    </el-tooltip>
                </template>
            </el-table-column>
        </el-table>
        <pagination
            v-show="total > 0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
        />

        <!-- 添加或修改用户配置对话框 -->
        <el-dialog :title="title" v-model="open" width="600px" append-to-body>
            <el-form
                :model="form"
                :rules="rules"
                ref="privRef"
                label-width="80px"
            >
                <el-col :span="12" v-if="form.id !== undefined">
                    <el-form-item label="权限编码" prop="id">
                        <el-input
                            v-model="form.id"
                            placeholder="请输入权限名称"
                            maxlength="30"
                            readonly
                        />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="权限名称" prop="name">
                        <el-input
                            v-model="form.name"
                            placeholder="请输入权限名称"
                            maxlength="30"
                        />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="权限类型" prop="type">
                        <el-select v-model="form.type" placeholder="请选择">
                            <el-option
                                v-for="dict in auth_entity_type"
                                :key="dict.value"
                                :label="dict.label"
                                :value="dict.value"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="状态">
                        <el-radio-group v-model="form.state">
                            <el-radio
                                v-for="dict in sys_normal_disable"
                                :key="dict.value"
                                :label="dict.value"
                                >{{ dict.label }}</el-radio
                            >
                        </el-radio-group>
                    </el-form-item>
                </el-col>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm"
                        >确 定</el-button
                    >
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup>
import { reactive, ref, toRefs, getCurrentInstance } from 'vue'
import { list, remove, add, edit, getById } from '@/api/system/auth'
const { proxy } = getCurrentInstance()
const { sys_normal_disable, auth_entity_type } = proxy.useDict(
    'sys_normal_disable',
    'auth_entity_type'
)

const data = reactive({
    form: {},
    tableData: [],
    queryParams: { name: '', state: '1', pageNum: 0, pageSize: 10 },
    open: false,
    title: '',
    total: 0,
    rules: {}
})
const single = ref(true)
const multiple = ref(true)
const ids = ref([])
const { queryParams, tableData, open, title, form, total, rules } = toRefs(data)
const loading = ref(false)
function handleQuery() {
    getList()
}
function getList() {
    loading.value = true
    list(queryParams.value).then((res) => {
        loading.value = false
        tableData.value = res.rows
        total.value = res.total
    })
}
function submitForm() {
    if (form.value.id != undefined) {
        edit(form.value).then((res) => {
            proxy.$modal.msgSuccess('修改成功')
            open.value = false
            getList()
        })
    } else {
        add(form.value).then((respone) => {
            proxy.$modal.msgSuccess('新增成功')
            open.value = false
            getList()
        })
    }
}
function cancel() {}
function handleDelete() {}
function handleAdd() {
    reset()
    title.value = '添加权限'
    open.value = true
}
function handleUpdate(row) {
    reset()
    title.value = '修改权限'
    const id = row.id || ids.value
    getById(id).then((res) => {
        form.value = res.data
        open.value = true
    })
}
function reset() {
    form.value = {
        id: undefined,
        name: '',
        type: '',
        state: '1'
    }
    proxy.resetForm('privRef')
}
getList()
</script>
<style scoped></style>
